package cn.javase.concurrent._设计模式._同步模式之顺序控制._固定运行顺序;

/**
 * 先输出2，再输出1
 * 使用wait和notify
 */
public class Test01 {
    private static boolean flag = false;
    private static final Object lock = new Object();
    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (lock) {
                while (!flag) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println(1);
            }
        }).start();
        new Thread(() -> {
            System.out.println(2);
            synchronized (lock) {
                flag = true;
                lock.notifyAll();
            }
        }).start();
    }
}
